Imports System.Text

''' <summary>
''' Validation helper class.
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class ValidationHelper
  ''' <summary>
  ''' Validates the length of a string.
  ''' </summary>
  ''' <param name="input">Text to validate.</param>
  ''' <param name="minimum">Minimum length.</param>
  ''' <param name="maximum">Maximum length.</param>
  ''' <returns>True of valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function CheckLength(ByVal input As String, ByVal minimum As Int32, ByVal maximum As Int32) As Boolean

    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    If (minimum < 0) Or (minimum > Int32.MaxValue) Then
      Throw New ArgumentOutOfRangeException("minimum")
    End If

    If (maximum < 0) Or (maximum > Int32.MaxValue) Then
      Throw New ArgumentOutOfRangeException("maximum")
    End If

    If (input.Length >= minimum) And (input.Length <= maximum) Then
      Return True
    Else
      Return False
    End If

  End Function

  ''' <summary>
  ''' Validates the length of a string.
  ''' </summary>
  ''' <param name="input">Text to validate.</param>
  ''' <param name="maximum">Maximum length.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function CheckLength(ByVal input As String, ByVal maximum As Int32) As Boolean

    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    If (maximum < 0) Or (maximum > Int32.MaxValue) Then
      Throw New ArgumentOutOfRangeException("maximum")
    End If

    If (input.Length > maximum) Then
      Return False
    Else
      Return True
    End If

  End Function

  ''' <summary>
  ''' Validates value is a number.
  ''' </summary>
  ''' <param name="input">Text to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Shared Function IsValidNumber(ByVal input As String) As Boolean

    If String.IsNullOrEmpty(input) Then
      Return False
    Else
      Return IsNumeric(input)
    End If

  End Function

  ''' <summary>
  ''' Validates value is a date.
  ''' </summary>
  ''' <param name="input">Text to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Shared Function IsValidDate(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return IsDate(input)

  End Function

  ''' <summary>
  ''' Validates if value is valid email address.
  ''' </summary>
  ''' <param name="input">Address to validate.</param>
  ''' <returns>Ture if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidEmail(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_EMAIL)

  End Function

  ''' <summary>
  ''' Validates if value is valid email address.
  ''' </summary>
  ''' <param name="input">Address to validate.</param>
  ''' <param name="expression">RegEx expression to use to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidEmail(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_EMAIL
    End If

    Return ValidateString(input, expression)

  End Function

  ''' <summary>
  ''' Validates if value is valid US Social Secuirty number.
  ''' </summary>
  ''' <param name="input">Social Security number to validate.</param>
  ''' <returns>True if valid Social Security number.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidSocialSecurity(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_US_SSN)

  End Function

  ''' <summary>
  ''' Validates if valus is valid Social Security number.
  ''' </summary>
  ''' <param name="input">Social Security number to validate.</param>
  ''' <param name="expression">RegEx expression to use to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidSocialSecurity(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_US_SSN
    End If

    Return ValidateString(input, expression)

  End Function

  ''' <summary>
  ''' Validates if value is valid US phone number.
  ''' </summary>
  ''' <param name="input">Phone nubmer to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidPhone(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_US_PHONE_NUMBER)
  End Function

  ''' <summary>
  ''' Validates if value is valid phone number.
  ''' </summary>
  ''' <param name="input">Phone number to validate.</param>
  ''' <param name="expression">RegEx expression to use to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidPhone(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_US_PHONE_NUMBER
    End If

    Return ValidateString(input, expression)

  End Function

  ''' <summary>
  ''' Validates if value is valid US zip code.
  ''' </summary>
  ''' <param name="input">Zip code to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidPostalCode(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    Return ValidateString(input, My.Resources.REGEXP_US_ZIP_CODE)
  End Function

  ''' <summary>
  ''' Validates if value is valid postal code, defaults to US zip code.
  ''' </summary>
  ''' <param name="input">Postal code to validate.</param>
  ''' <param name="expression">RegEx expression to use to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidPostalCode(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_US_ZIP_CODE
    End If

    Return ValidateString(input, expression)

  End Function

  ''' <summary>
  ''' Validates if value is valid URL.
  ''' </summary>
  ''' <param name="input">URL to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidUrl(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_URL)

  End Function

  ''' <summary>
  ''' Validates if value is a valid URL.
  ''' </summary>
  ''' <param name="input">URL to validate.</param>
  ''' <param name="expression">RegEx expression to use to validate.</param>
  ''' <returns>True if valid.</returns>
  ''' <remarks></remarks>
  Public Overloads Shared Function IsValidUrl(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_URL
    End If

    Return ValidateString(input, expression)

  End Function

  Public Overloads Shared Function IsValidDomain(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_DOMAIN)

  End Function

  Public Overloads Shared Function IsValidDomain(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_DOMAIN
    End If

    Return ValidateString(input, expression)

  End Function


  Public Overloads Shared Function IsValidFilePath(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_FILE_PATH)

  End Function

  Public Overloads Shared Function IsValidFilePath(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_FILE_PATH
    End If

    Return ValidateString(input, expression)

  End Function

  Public Overloads Shared Function IsValidFirstLastName(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_FIRST_LAST_NAME)

  End Function

  Public Overloads Shared Function IsValidFirstLastName(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_FIRST_LAST_NAME
    End If

    Return ValidateString(input, expression)

  End Function


  Public Overloads Shared Function IsValidTemperature(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_TEMP)

  End Function

  Public Overloads Shared Function IsValidTemperature(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_TEMP
    End If

    Return ValidateString(input, expression)

  End Function

  Public Overloads Shared Function IsValidScientific(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_SCIENENTIFIC)

  End Function

  Public Overloads Shared Function IsValidScientific(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_SCIENENTIFIC
    End If

    Return ValidateString(input, expression)

  End Function

  Public Overloads Shared Function IsValidIsbn(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_ISBN)

  End Function

  Public Overloads Shared Function IsValidIsbn(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_ISBN
    End If

    Return ValidateString(input, expression)

  End Function

  Public Overloads Shared Function IsValidCreditCard(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_CREDIT_CARD)

  End Function

  Public Overloads Shared Function IsValidCreditcard(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_CREDIT_CARD
    End If

    Return ValidateString(input, expression)

  End Function

  ''' <summary>
  ''' Determines whether [is valid string] [the specified input].
  ''' </summary>
  ''' <param name="input">The text to validate.</param>
  ''' <returns>
  ''' <c>true</c> if [is valid string] [the specified input]; otherwise, <c>false</c>.
  ''' </returns>
  Public Overloads Shared Function IsValidString(ByVal input As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    Return ValidateString(input, My.Resources.REGEXP_STRING)

  End Function

  ''' <summary>
  ''' Determines whether [is valid string] [the specified input].
  ''' </summary>
  ''' <param name="input">The text to validate.</param>
  ''' <param name="expression">RegEx expression to use for validation.</param>
  ''' <returns>
  ''' <c>true</c> if [is valid string] [the specified input]; otherwise, <c>false</c>.
  ''' </returns>
  Public Overloads Shared Function IsValidString(ByVal input As String, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If
    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_STRING
    End If

    Return ValidateString(input, expression)

  End Function

  ''' <summary>
  ''' Determines whether [is valid string] [the specified input].
  ''' </summary>
  ''' <param name="input">The text to validate.</param>
  ''' <param name="safeForWeb">if set to <c>true</c> [safe for web].</param>
  ''' <returns>
  ''' <c>true</c> if [is valid string] [the specified input]; otherwise, <c>false</c>.
  ''' </returns>
  Public Overloads Shared Function IsValidString(ByVal input As String, ByVal safeForWeb As Boolean) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    If safeForWeb Then
      If ValidateString(input, My.Resources.REGEXP_STRING) Then
        Return Not ValidateString(input, My.Resources.REGEXP_STRING_WEB_SAFE)
      End If
    Else
      Return ValidateString(input, My.Resources.REGEXP_STRING)
    End If

  End Function

  ''' <summary>
  ''' Determines whether [is valid string] [the specified input].
  ''' </summary>
  ''' <param name="input">The text to validate.</param>
  ''' <param name="safeForWeb">if set to <c>true</c> [safe for web].</param>
  ''' <param name="expression">RegEx expression to use for validation.</param>
  ''' <returns>
  ''' <c>true</c> if [is valid string] [the specified input]; otherwise, <c>false</c>.
  ''' </returns>
  Public Overloads Shared Function IsValidString(ByVal input As String, ByVal safeForWeb As Boolean, ByVal expression As String) As Boolean
    If String.IsNullOrEmpty(input) Then
      Throw New ArgumentNullException("input")
    End If

    If String.IsNullOrEmpty(expression) Then
      expression = My.Resources.REGEXP_STRING
    End If
    If safeForWeb Then
      If ValidateString(input, expression) Then
        Return Not ValidateString(input, My.Resources.REGEXP_STRING_WEB_SAFE)
      End If
    Else
      Return ValidateString(input, expression)
    End If

  End Function

  ''' <summary>
  ''' Validates the string.
  ''' </summary>
  ''' <param name="input">The text to validate.</param>
  ''' <param name="expression">RegEx expression to use for validation.</param>
  ''' <returns>True if valid.</returns>
  Friend Shared Function ValidateString(ByVal input As String, ByVal expression As String) As Boolean
    Dim validator As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(expression)
    Return validator.IsMatch(input)
  End Function

  Private Sub New()
  End Sub

End Class
